package errHandler

import (
	"fmt"
	web "geektime-go-homework/web/class2"
)

type MiddlewareBuilder struct {
	resp map[int][]byte
}

func NewMiddlewareBuilder() *MiddlewareBuilder {
	return &MiddlewareBuilder{
		resp: map[int][]byte{},
	}
}

func (m *MiddlewareBuilder) AddCode(code int, content []byte) *MiddlewareBuilder {
	m.resp[code] = content
	return m
}

func (m *MiddlewareBuilder) Build() web.Middleware {
	return func(next web.HandleFunc) web.HandleFunc {
		return func(ctx *web.Context) {
			next(ctx)
			resp, ok := m.resp[ctx.RespStatusCode]
			fmt.Println(ok)
			fmt.Printf("status code is %d", ctx.RespStatusCode)
			if ok {
				ctx.RespData = resp
			}
		}
	}
}
